<template>
  <div>
    <div class="mg-10">
      <el-input style="width: 200px" suffix-icon="el-icon-search" placeholder="请输入名称" v-model="filename"/>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="refresh">重置</el-button>
    </div>
    <div class="mg-10">
      <el-upload action="http://localhost:8080/file/upload" :show-file-list="false" :on-success="handleUploadSuccess"
                 style="display: inline-block">
        <el-button type="primary" icon="el-icon-circle-plus-outline">上传文件</el-button>
      </el-upload>
      <el-popconfirm
          style="margin: 0px 10px"
          confirm-button-text='确认'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="确定要批量删除吗？"
          @confirm="deleteBatch"
      >
        <el-button type="danger" icon="el-icon-remove-outline" slot="reference">批量删除</el-button>
      </el-popconfirm>
    </div>
    <el-table :data="tableData" border stripe @selection-change="handleSelectionChange"
              :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="60px" v-if="false">
      </el-table-column>
      <el-table-column prop="name" label="文件名称" width="auto" >
      </el-table-column>
      <el-table-column prop="type" label="文件类型" width="100px" align="center">
      </el-table-column>
      <el-table-column prop="size" label="文件大小(kb)" width="100px"align="center">
      </el-table-column>
      <el-table-column label="下载链接" width="100px" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="download(scope.row.url)">下载</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="启用" width="80px" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="changeState(scope.row)"
              v-model="scope.row.enable"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100px" align="center">
        <template slot-scope="scope">
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确认'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定要删除吗？"
              @confirm="handleDelete(scope.row.id)"
          >
            <el-button
                size="mini"
                type="danger"
                slot="reference"
            >删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pd-10">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "Files",
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      filename:'',
      total: 0,
      pageNum: 1,
      pageSize: 5,
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      axios.get("/file/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          filename: this.filename
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    handleDelete(row) {
      axios.delete("/file/del/" + row).then(res => {
        if (res.data.code='200') {
          this.$message.success(res.data.msg)
        } else {
          this.$message.error("删除失败")
        }
        this.load()
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      axios.post("/file/del/batch", ids).then(res => {
        if (res.data.code='200') {
          this.$message.success(res.data.msg)
        } else {
          this.$message.error("批量删除失败")
        }
        this.load()
      })
    },
    refresh(){
      this.filename=''
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleUploadSuccess() {
      this.$message.success("导入成功！")
      this.load()
    },
    download(url){
      window.open(url)
    },
    changeState(row){
      axios.post("/file/save",row).then(res=>{
        if(res.data.code=='200'){
          this.$message.success(res.data.msg)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>